package com.enjoyireland.hiking;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

/**
 * 
 * @author Rónan Mac an tSaoir
 *
 */
public class WalkFragment extends Fragment implements OnTabChangeListener {

	public static final String NAME = "WalkList";
	private static final String TAB_WALK = "Walk Details";
	private static final String TAB_MAP = "View Map";

	private View _root;
	private TabHost _tabHost;
	private int _currentTab = 0;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	/**
	 * Update layout of Fragment with TabHost
	 * Insert Tabs
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		setHasOptionsMenu(true);
		FragmentManager fm = getFragmentManager();
		if (fm.findFragmentById(R.id.walkTab) == null) {
			fm.beginTransaction().replace(R.id.walkTab, new WalkDetails()).commit();
			fm.beginTransaction().replace(R.id.mapTab, new WalkView()).commit();
		}
		_root = inflater.inflate(R.layout.tabs_fragment, null);
		_tabHost = (TabHost) _root.findViewById(android.R.id.tabhost);
		setup();
		return _root;
	}
	
	/**
	 * Load tab content
	 * N.B. Preserve state of tab
	 * 
	 */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setRetainInstance(true);
		_tabHost.setOnTabChangedListener(this);
		_tabHost.setCurrentTab(_currentTab);
		/* automatically start loading walk details */
		updateTab(TAB_WALK, R.id.walkTab);
	}

	/**
	 * Create new tab with specified layout details
	 * 
	 * @param tag
	 * @param labelId
	 * @param tabContentId
	 * 		
	 * @return
	 */
	private TabSpec newTab(String tag, int labelId, int tabContentId) {
		View v = LayoutInflater.from(getActivity()).inflate(R.layout.tab,(ViewGroup) _root.findViewById(android.R.id.tabs), false);
		((TextView) v.findViewById(R.id.text)).setText(labelId);

		TabSpec tabSpec = _tabHost.newTabSpec(tag);
		tabSpec.setIndicator(v);
		tabSpec.setContent(tabContentId);
		return tabSpec;
	}

	/**
	 * Run setup for tabHost
	 */
	private void setup() {
		_tabHost.setup();
		_tabHost.addTab(newTab(TAB_WALK, R.string.tab_walk, R.id.walkTab));
		_tabHost.addTab(newTab(TAB_MAP, R.string.tab_map, R.id.mapTab));
	}

	/**
	 * Change active tab
	 * 
	 * @param tabId
	 * @param placeholder
	 */
	private void updateTab(String tabId, int placeholder) {
		FragmentManager fm = getFragmentManager();
		if (placeholder == R.id.walkTab && fm.findFragmentByTag(tabId) == null) {
			fm.beginTransaction().replace(placeholder, new WalkDetails(), tabId).commit();
		}
		else if (placeholder == R.id.walkTab && fm.findFragmentByTag(tabId) == null) {
			fm.beginTransaction().replace(placeholder, new WalkView(), tabId).commit();
		}
	}

	@Override
	public void onTabChanged(String arg0) {
		if (TAB_WALK.equals(arg0)) {
			updateTab(arg0, R.id.walkTab);
			_currentTab = 0;
			return;
		}
		if (TAB_MAP.equals(arg0)) {
			updateTab(arg0, R.id.mapTab);
			_currentTab = 1;
			return;
		}
	}
}
